﻿Imports System.Collections.Generic
Imports System.Linq
Partial Class UpdateProfile
    Inherits System.Web.UI.Page
    Dim cUser As OneCall.User.AuthUser
    Dim SQLDB As New OneCall.DataModel.OneCDataEntities
    Dim cCustomer As OneCall.DataModel.Customer

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        cUser = New OneCall.User.AuthUser(Request, Response)
        If cUser.isActiveSession() Then
           
        Else
            
        End If

        Dim req As String = Request("req")

        If req = "password" Then
            panelPassword.Visible = True
        End If
        If req = "email" Then
            panelEmail.Visible = True
            Dim iniTime As String = Now().TimeOfDay.ToString()
            txtIniTime.Value = iniTime
            Dim regkey As String = OneCall.Crypt.Encrypt(iniTime, "1CHCAREREGNEWCUST")
            regkey = Left(regkey, 6)
            imgCap.ImageUrl = "capimg.ashx?str=" & regkey


            Dim uid = cUser.GetUserID()

            cCustomer = (From p In SQLDB.Customers Where p.Customer_ID = uid Select p).ToArray(0)

            txtCEmailID.Text = cCustomer.Customer_EmailID

        End If

    End Sub

    Protected Sub btnChangePassword_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChangePassword.Click
        '//validate
        lblPassConfirm.Visible = False
        Dim uid = cUser.GetUserID()
        cCustomer = (From p In SQLDB.Customers Where p.Customer_ID = uid Select p).ToArray(0)

        If txtcPassword.Text = "" Then
            lblerrxpassword.Visible = True
            cCustomer = Nothing
            SQLDB.Dispose()
            Exit Sub
        Else
            If Not cCustomer.Customer_Password = txtcPassword.Text Then
                lblerrxpassword.Visible = True
                cCustomer = Nothing
                SQLDB.Dispose()
                Exit Sub
            Else
                lblerrxpassword.Visible = False
            End If
        End If

        If txtNewPassword.Text = txtRePassword.Text And Not txtNewPassword.Text = "" Then
            lblerrrpassword.Visible = False
            lblerrnpassword.Visible = False
            cCustomer.Customer_Password = txtNewPassword.Text
            SQLDB.SaveChanges()
            lblPassConfirm.Visible = True
            Dim msg As String = ""
            msg = msg & "Hi " & cCustomer.Customer_FirstName & " " & cCustomer.Customer_LastName & "<br>"
            msg = msg & "<br>"
            msg = msg & "You password has been chnaged successfully." & "<br>" & "<br>"
            msg = msg & "Your new password is give below" & "<br>"
            msg = msg & "==============================================" & "<br>"
            msg = msg & cCustomer.Customer_Password & "<br>"
            msg = msg & "==============================================" & "<br>"
            msg = msg & "" & "<br>"
            msg = msg & "Regards," & "<br>"
            msg = msg & "Customer service" & "<br>"
            msg = msg & "One Call Home Care Solution" & "<br>"
            msg = msg & "www.onecallhomecaresolution.com" & "<br>"
            OneCall.EmailGateway.EMailSender.SendWebEmail(cCustomer.Customer_EmailID, "Password changed", msg)
            Dim smsmessage As String = "Hi " & cCustomer.Customer_FirstName & " " & cCustomer.Customer_LastName & " your login password has been changed, your new password is " & cCustomer.Customer_Password
            OneCall.SMSGateway.SendSMS(smsmessage, cCustomer.Customer_Mobile)
        Else
            lblerrrpassword.Visible = True
            lblerrnpassword.Visible = True

        End If
        cCustomer = Nothing
        SQLDB.Dispose()

    End Sub

    Protected Sub btnUpdateEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdateEmail.Click

        If txtNewEmail.Text = "" Or checkEmail(txtNewEmail.Text) = False Then
            lblerremailid.Visible = True
            genCaptcha()
        Else
            lblerremailid.Visible = False
            '//check captcha
            Dim str As String = txtIniTime.Value
            str = OneCall.Crypt.Encrypt(str, "1CHCAREREGNEWCUST")
            str = Left(str, 6)
            If (str = txtCaptcha.Text) Then
                cCustomer.Customer_EmailID = txtNewEmail.Text
                SQLDB.SaveChanges()
                SQLDB.Dispose()
                lblerrcaptachemail.Visible = False
                lblemailsuccess.visible = True
            Else
                genCaptcha()
                lblerrcaptachemail.Visible = True
            End If

        End If
    End Sub
    Private Sub genCaptcha()

        Dim iniTime As String = Now().TimeOfDay.ToString()
        txtIniTime.Value = iniTime
        Dim regkey As String = OneCall.Crypt.Encrypt(iniTime, "1CHCAREREGNEWCUST")
        regkey = Left(regkey, 6)
        imgCap.ImageUrl = "capimg.ashx?str=" & regkey
    End Sub

    Private Function checkEmail(ByVal str As String) As Boolean
        Dim email As New Net.Mail.MailMessage

        Try
            email.To.Add(str)
            Return True
        Catch ex As Exception
            Return False
        End Try


    End Function
   
End Class
